﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PodDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPodDetailDA PodDetailDA = DataAccess.CreatePodDetailDA();
        private PubParasInfo pubParasInfo;

        public PodDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public PodDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PodDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<PodDetailInfo> podDetailInfos = new List<PodDetailInfo>();
                PodDetailInfo podDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    podDetailInfo = PodDetailDA.GetPodDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0]);
                    podDetailInfos.Add(podDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (PodDetailDA.DeletePodDetail(dataBase, tran, mainID, subId, this.dataBaseID) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PodDetailInfo>(dataBase, tran, podDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "POD_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return PodDetailDA.GetAllPodDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(PodDetailQueryEntity PoddetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(PoddetailQuery.PodID))
            {
                sqlWhere.Add(" POD_ID " + PoddetailQuery.PodID);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.LineID))
            {
                sqlWhere.Add(" LINE_ID " + PoddetailQuery.LineID);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.BolLineID))
            {
                sqlWhere.Add(" BOL_LINE_ID " + PoddetailQuery.BolLineID);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.DeliveryID))
            {
                sqlWhere.Add(" DELIVERY_ID " + PoddetailQuery.DeliveryID);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.DeliveryLineID))
            {
                sqlWhere.Add(" DELIVERY_LINE_ID " + PoddetailQuery.DeliveryLineID);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.Status))
            {
                sqlWhere.Add(" STATUS " + PoddetailQuery.Status);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.AcceptedQty))
            {
                sqlWhere.Add(" ACCEPTED_QTY " + PoddetailQuery.AcceptedQty);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.RejectedQty))
            {
                sqlWhere.Add(" REJECTED_QTY " + PoddetailQuery.RejectedQty);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.LoseQty))
            {
                sqlWhere.Add(" LOSE_QTY " + PoddetailQuery.LoseQty);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.BreakageQty))
            {
                sqlWhere.Add(" BREAKAGE_QTY " + PoddetailQuery.BreakageQty);
            }
            if (!string.IsNullOrEmpty(PoddetailQuery.PkgBreakageQty))
            {
                sqlWhere.Add(" PKG_BREAKAGE_QTY " + PoddetailQuery.PkgBreakageQty);
            }
            return PodDetailDA.GetPodDetailByQueryList(sqlWhere, PoddetailQuery, this.dataBaseID[0]);
        }

        public PodDetailInfo GetDetailInfoByID(string podID, string lineID, string pk3)
        {
            return PodDetailDA.GetPodDetailByID(podID, lineID, this.dataBaseID[0]);
        }

        public List<PodDetailInfo> GetDetailInfoListByID(string podID)
        {
            return PodDetailDA.GetPodDetailListByID(podID, this.dataBaseID[0]);
        }

        public string InsertDetail(object obj)
        {
            string PodDetailID = string.Empty;
            PodDetailInfo podDetailInfo = obj as PodDetailInfo;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (PodDetailDA.CheckPodDetailIDUnique(dataBase, tran, podDetailInfo.PodID, podDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8500_002", this.pubParasInfo.UserLanguage, new List<string> { podDetailInfo.PodID, podDetailInfo.LineID.ToString() });
                }
                if (podDetailInfo == null)
                {
                    return PodDetailID;
                }
                podDetailInfo.UpdatedBy = podDetailInfo.CreatedBy;
                podDetailInfo.CreatedDate = DateTime.Now;
                podDetailInfo.UpdatedDate = DateTime.Now;
                if (PodDetailDA.InsertPodDetail(dataBase, tran, podDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PodDetailInfo>(null, podDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "POD_DETAIL", this.dataBaseID[0]);
                    PodDetailID = podDetailInfo.PodID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return PodDetailID;
        }

        public string UpdateDetail(object obj)
        {
            string PodDetailID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                PodDetailInfo podDetailInfo = obj as PodDetailInfo;
                PodDetailInfo oldPodDetailInfo = PodDetailDA.GetPodDetailByID(dataBase, tran, podDetailInfo.PodID, podDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (PodDetailDA.UpdatePodDetail(podDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PodDetailInfo>(oldPodDetailInfo, podDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "POD_DETAIL", this.dataBaseID[0]);
                    PodDetailID = podDetailInfo.PodID + "," + podDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return PodDetailID;
        }
    }
}

